package com.bizdk.generator.controller;

import cn.hutool.core.util.StrUtil;
import com.bizdk.generator.common.query.Query;
import com.bizdk.generator.utils.ServletUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 基类管理
 */
public abstract class BaseController {
    
    /**
     * 获取request
     */
    public HttpServletRequest getRequest() {
        return ServletUtils.getRequest();
    }
    
    /**
     * 获取response
     */
    public HttpServletResponse getResponse() {
        return ServletUtils.getResponse();
    }
    
    /**
     * 获取session
     */
    public HttpSession getSession() {
        return getRequest().getSession();
    }
    
    /**
     * 页面跳转
     */
    public String redirect(String url) {
        return StrUtil.format("redirect:{}", url);
    }
    
    
    protected abstract String getPrefix();
    
    @GetMapping("/list")
    public ModelAndView list(Query query) {
        return new ModelAndView(getPrefix() + "/list");
    }
    
    @GetMapping("/add")
    public ModelAndView add() {
        return new ModelAndView(getPrefix() + "/add");
    }
    
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id") Long id) {
        return new ModelAndView(getPrefix() + "/edit");
    }
}